package com.ithiema.edu.info.manager.dao;

import com.ithiema.edu.info.manager.domain.Student;

import com.ithiema.edu.info.manager.utils.Utils;

public class StudentDao {
    //定义静态学生类数组
    private static Student[] stus = new Student[5];
    //创建工具类对象
    Utils utils = new Utils();
    static {
        stus[0] = new Student("heima01","伊一","女","1997-09-16");
        stus[1] = new Student("heima02","伞伞","男","1995-07-12");
        stus[2] = new Student("heima03","姒姒","女","1996-02-28");
    }
    //添加学员方法
    public boolean addStudent(Student stu) {
        //定义变量index，当作null的索引值
        int index = -1;
        boolean flag = false;
        //遍历数组，判断数组有无null，若有，返回索引值，跳出
        for (int i = 0; i < stus.length; i++) {
            if (stus[i] == null){
                index = i;
                break;
            }
        }
        //若表示数组中为null的索引值不为-1，则添加学员信息
        if(index != -1){
        stus[index] = stu;
            flag = true;   //添加成功，返回turn
        }
        return flag;
    }

    //删除学员信息
    public boolean deleteStudent(String id) {
        boolean flag = false;
        //查询相同学号索引
        int index = utils.InquireIndex(id,stus);
        if (index != -1) {
            //删除学员信息
            stus[index] = null;
            flag = true;
        }
        return flag;
    }

    //返回学生数组类
    public Student[] findAllStudent() {
        return stus;
    }


    //修改学员信息
    public void reviseStudent(String id, Student stu) {
        //查询学号对应的索引
        int index = utils.InquireIndex(id,stus);
        stus[index] = stu;
    }
}
